package com.tcm.config;

import com.tcm.common.Interceptor.WebSocketAuthInterceptor;
import com.tcm.common.handler.WebSocketHandler;
import io.micrometer.core.lang.NonNullApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * webSocket配置类
 */
@Slf4j
@NonNullApi
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketHandler webSocketHandler;

    /**
     * 注册WebSocket处理器
     * @return WebSocket处理器
     */
    @Bean
    public WebSocketHandler battleWebSocketHandler(){
        return new WebSocketHandler();
    }

    /**
     * 注册WebSocket拦截器
     * @return WebSocket拦截器
     */
    @Bean
    public WebSocketAuthInterceptor webSocketAuthInterceptor(){
        return new WebSocketAuthInterceptor();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler, "/ws/pk")
                .addInterceptors(webSocketAuthInterceptor())
                .setAllowedOrigins("*");
        log.info("注册WebSocket权限验证拦截器");
    }
}
